package com.sgb.kjwl.viewmodel.self.publish;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.user.AddPackersEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.publish.PackersPublishActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.List;

import io.reactivex.functions.Consumer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PackersPublishModel extends BaseViewModel {
    public ObservableField<AddPackersEntity> addPackersEntity = new ObservableField<>(new AddPackersEntity());
    public ObservableField<String> typeContent = new ObservableField<>("");
    private PackersPublishActivity activity;
    private SelfRepertory repertory;

    public PackersPublishModel(PackersPublishActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);

        APIManager.getInstance().getGoodsAPI().getLocateList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getCode().equals("200")) {
                    activity.showAddressView(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }

            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    public void getDetailModel(String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getPackersDetail(id)
                .subscribe(new Consumer<BaseEntity<AddPackersEntity>>() {
                    @Override
                    public void accept(BaseEntity<AddPackersEntity> addPackersEntityBaseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        if (addPackersEntityBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.showEditView(addPackersEntityBaseEntity.getData(), true);
                        } else {
                            MToast.showToast(activity, addPackersEntityBaseEntity.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }
}
